<template>
  <el-dialog :close-on-click-modal='false' title="资讯" :visible.sync="dialogFormVisible" width="90%" :before-close="cancel">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="5">
          <el-form-item label="资讯标题：" prop="title">
            <el-input v-model="form.title" placeholder="请输入资讯标题"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="咨询板块：" prop="marketPlateId">
            <el-select v-model="form.marketPlateId" placeholder="请选择">
              <el-option v-for="marke, index in marketOptionsList" :key="index" :label="marke.label"
                :value="marke.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="文章简介：" prop="introduce">
            <el-input v-model="form.introduce" placeholder="请输入文章简介"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="资讯正文：" prop="content">
            <Editor v-model="form.content" :quillIndex="20" toref="Editorc" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import Editor from '@/components/Quill'//引入组件内容并注册
export default {
  props: {
    marketOptionsList: {
      type: Array,
      required: false
    },
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: false
    },
    api: {
      type: Function,
      required: true
    }
  },
  components: {
    Editor
  },
  data () {
    return {
      value: '',
      editorText: '',
      dialogFormVisible: false,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        marketPlateId: [
          { required: true, message: '请选择板块', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请编辑内容', trigger: 'change' }
        ],
      },
      form: {
        title: '',
        bankuai: '',
        content: ''
      },
    };
  },
  watch: {
    "form.content": {
      handler (v) {
        v && this.$nextTick(() => {
          this.$refs['form'].clearValidate(['content']);
        })
      },
      deep: true
    },
    showDialog: {
      handler (v) {
        this.dialogFormVisible = v
        this.formData.marketPlate ? this.formData.marketPlateId = this.formData.marketPlate.marketPlateId : ''
        v && (this.form = { ...this.form, ...this.formData })
      }
    }
  },
  methods: {
    //关闭 isRef是否刷新表单
    close (isRef) {
      this.$emit('closeDialog', isRef)
      this.$refs.form.resetFields()
    },
    //取消
    cancel () {
      this.form = {
        nickName: '',
        userName: '',
        phoneNumber: '',
        email: '',
        userType: "platform_user"
      }
      this.close()
    },
    //确认
    confirm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //  this.api 添加编辑接口 （注意传参）
          this.api(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.cancel()
              this.close(true)
            } else {
              this.$message({
                type: 'info',
                message: res.msg || '操作失败!'
              });
            }
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-col-5 {
  width: 400px;

  .el-select {
    width: 100%;
  }
}
</style>